package com.highspeed.bdk.service.security.role;

import com.bianmaba.spring.data.jpa.service.BaseService;
import com.highspeed.bdk.bean.entity.security.Role;
import com.highspeed.bdk.repository.security.role.RoleRepository;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
/**
 * Created by chenjiabin on 2018/3/23.
 */
@Service
@Transactional
public class RoleService extends BaseService<Role, String> implements IRoleService {
    private static final Log LOG = LogFactory.getLog(RoleService.class);
    @Autowired
    private RoleRepository repository;

    @Override
    public RoleRepository getRepository() {
        return repository;
    }

    @Override
    public <S extends Role> S save(S entity) {
        if (StringUtils.isEmpty(entity.getId())) {
            String user = SecurityContextHolder.getContext().getAuthentication().getName();
            entity.setCreator(user);
        }
        return super.save(entity);
    }

    @Override
    public <S extends Role> List<S> save(Iterable<S> entities) {
        for (S entity : entities) {
            if (StringUtils.isEmpty(entity.getId())) {
                String user = SecurityContextHolder.getContext().getAuthentication().getName();
                entity.setCreator(user);
            }
        }
        return super.save(entities);
    }
}
